package com.crazy.base.starter.mybatisplus.injector.method;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.crazy.base.starter.mybatisplus.injector.InjectedMethod;
import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.executor.keygen.NoKeyGenerator;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

import java.util.List;

public class BatchInsert extends AbstractMethod {
    public BatchInsert() {
        super(InjectedMethod.BATCH_INSERT.getMethod());
    }

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo table) {

        StringBuilder fieldBuilder = new StringBuilder();
        //添加插入值数据
        StringBuilder subSqlBuild = new StringBuilder();
        StringBuilder valuesBuild = new StringBuilder();

        fieldBuilder.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n");
        String keyProperty = null;
        String keyColumn = null;
        KeyGenerator keyGenerator = null;
        // 表包含主键处理逻辑,如果不包含主键当普通字段处理
        if (table.getKeyProperty() != null && !table.getKeyProperty().isBlank()) {
            keyGenerator = new NoKeyGenerator();
            if (table.getIdType() == IdType.AUTO) {
                /* 自增主键 */
                keyGenerator = new Jdbc3KeyGenerator();
                keyProperty = table.getKeyProperty();
                keyColumn = table.getKeyColumn();
            } else {
                if (null != table.getKeySequence()) {
                    keyGenerator = TableInfoHelper.genKeyGenerator(InjectedMethod.BATCH_INSERT.getMethod(), table, builderAssistant);
                    keyProperty = table.getKeyProperty();
                    keyColumn = table.getKeyColumn();
                    fieldBuilder.append(table.getKeyColumn()).append(",");
                    valuesBuild.append("#{item.").append(table.getKeyProperty()).append("},");
                } else {
                    /* 用户输入自定义ID */
                    fieldBuilder.append(table.getKeyColumn()).append(",");
                    // 正常自定义主键策略
                    valuesBuild.append("#{item.").append(table.getKeyProperty()).append("},");
                }
            }
        }
        List<TableFieldInfo> fieldList = table.getFieldList();
        for (TableFieldInfo fieldInfo : fieldList) {
            fieldBuilder.append(fieldInfo.getColumn()).append(",");
            valuesBuild.append("#{item.").append(fieldInfo.getEl()).append("},");
        }
        subSqlBuild.append("\n<foreach item=\"item\" index=\"index\" collection=\"list\" separator=\",\">");
        subSqlBuild.append("\n<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">\n").append(valuesBuild).append("\n</trim>");
        subSqlBuild.append("\n</foreach>");

        fieldBuilder.append("\n</trim>");
        String sql = String.format(InjectedMethod.BATCH_INSERT.getSql(), table.getTableName(), fieldBuilder, subSqlBuild);
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        return addInsertMappedStatement(mapperClass, modelClass, super.methodName, sqlSource, keyGenerator, keyProperty, keyColumn);
    }
}
